<?php
namespace ff\helpers;

class StringLib
{

    public static function randString($length = 5, $mod = 'UMN', $starthash = '')
    {
        $hash = $starthash;

        if (preg_match("/U/i", $mod)) {
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        }
        if (preg_match("/M/i", $mod)) {
            $chars .= 'abcdefghijklmnopqrstuvwxyz';
        }
        if (preg_match("/N/i", $mod)) {
            $chars .= '0123456789';
        }
        $max = strlen($chars) - 1;
        if (PHP_VERSION < '4.2.0') {
            mt_srand((double) microtime() * 1000000);
        }
        for ($i = 0; $i < $length; $i++) {

            $hash .= $chars[mt_rand(0, $max)];
            if ($i == 0) {
                $hash = ($hash[0] == '0') ? '1' : $hash;
            }
        }
        return $hash;
    }

    public static function myEncrypt($string, $action = 'EN', $auth = '')
    {
        $string = strval($string);
        if ($string == '') {
            return '';
        }

        if ($action == 'EN') {
            $strauth = substr(md5($string), 8, 10);
        } else {
            $strauth = substr($string, -10);
            $string = base64_decode(substr($string, 0, strlen($string) - 10));
        }
        $key = md5($strauth . $auth);
        $len = strlen($key);
        $code = '';
        for ($i = 0; $i < strlen($string); $i++) {
            $k = $i % $len;
            $code .= $string[$i] ^ $key[$k];
        }
        $code = ($action == 'DE' ? (substr(md5($code), 8, 10) == $strauth ? $code : null) : base64_encode($code) . $strauth);
        return $code;
    }

    public static function getArySign($array, $authkey = 'bigqi_com', $authmode = 'md5')
    {
        global $_SIGNSTR;
        if (is_array($array)) {
            if ($array['sign']) {
                unset($array['sign'], $array['sign_type']);
            }
            ksort($array);
            reset($array);
            $sign = '';
            foreach ($array as $key => $value) {
                if ($value != '') {
                    $sign .= "$key=$value&";
                }
            }
            $_SIGNSTR = substr($sign, 0, -1);
            $sign = substr($sign, 0, -1) . $authkey;

            $authmodeary = array('md5', 'num6');

            $authmode = in_array($authmode, $authmodeary) ? $authmode : current($authmodeary);

            if ($authmode == 'md5') {
                return md5($sign);
            } elseif ($authmode == 'num6') {
                return substr(sprintf("%u", crc32($sign)), 0, 6);
            }

        } else {
            return false;
        }
    }

    public static function strColor($str)
    {
        $strcode = md5($str);
        $hexcode = '';
        for ($i = 0; $i < strlen($strcode); $i++) {
            $hexcode .= base_convert($strcode[$i], 36, 16);
        }
        $colorhexcode = substr($hexcode, 0, 6);
        return '#' . $colorhexcode;
    }

    public static function strBytes($val)
    {
        $val = trim($val);
        $last = strtolower($val{strlen($val) - 1});
        switch ($last) {
            case 'g':$val *= 1024;
            case 'm':$val *= 1024;
            case 'k':$val *= 1024;
        }
        return $val;
    }

}
